package jmu.cze.controller;

import jmu.cze.pojo.AircraftModel;
import jmu.cze.pojo.Flight;
import jmu.cze.pojo.OnFlight;
import jmu.cze.service.FlightService;
import jmu.cze.utl.ResultUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

@Controller
@RequestMapping("/admin")
public class FlightController {
    private FlightService flightService;

    @Resource(name = "flightServiceImpl")
    public void setFlightService(FlightService flightService) {
        this.flightService = flightService;
    }

    @GetMapping("/flight")
    public String flight(Model model){
        model.addAttribute("terminalMap", flightService.selectAllTerminals());
        model.addAttribute("aircraftModelMap", flightService.selectAllAircraftModels());
        model.addAttribute("flights", flightService.selectAllFlights());
        return "flight";
    }

    @PostMapping("/addAircraftModel")
    @ResponseBody
    public String addAircraftModel(AircraftModel aircraftModel){
        return ResultUtil.getResult(flightService.insertAircraftModel(aircraftModel));
    }

    @PostMapping("/updateAircraftModel")
    @ResponseBody
    public String updateAircraftModel(AircraftModel aircraftModel){
        return ResultUtil.getResult(flightService.updateAircraftModel(aircraftModel));
    }

    @GetMapping("/deleteAircraftModel/{id}")
    @ResponseBody
    public String deleteAircraftModel(@PathVariable("id") int id){
        return ResultUtil.getResult(flightService.deleteAircraftModelByID(id));
    }

    @PostMapping("/addFlight")
    @ResponseBody
    public String addFlight(Flight flight) {
        return ResultUtil.getResult(flightService.insertFlight(flight));
    }

    @PostMapping("/updateFlight")
    @ResponseBody
    public String updateFlight(Flight flight){
        return ResultUtil.getResult(flightService.updateFlight(flight));
    }

    @GetMapping("/deleteFlight/{id}")
    @ResponseBody
    public String deleteFlight(@PathVariable("id") int id){
        return ResultUtil.getResult(flightService.deleteFlightByID(id));
    }
}
